<template>
	<!--我的企业介绍-->
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table
				:data="list.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
				border
				row-key="_id"
				:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
				 style="width: 100%;">
			<el-table-column prop="department" label="部门" min-width="120" sortable></el-table-column>
			<el-table-column prop="telNumber" label="手机号" min-width="120" sortable></el-table-column>
			<el-table-column prop="name" label="名称" min-width="120" sortable></el-table-column>
			<el-table-column label="操作" width="250">
				<template slot="header" slot-scope="scope">
					<el-input
							v-model="search"
							size="mini"
							placeholder="输入名称搜索"/>
				</template>
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px"  ref="editForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="telNumber">
					<el-input type="textarea" v-model="editForm.telNumber" auto-complete="off" :rows="2"></el-input>
				</el-form-item>
				<el-form-item label="部门" prop="department">
					<el-input type="textarea" v-model="editForm.department" auto-complete="off" :rows="2"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="telNumber">
					<el-input v-model="addForm.telNumber" auto-complete="off" ></el-input>
				</el-form-item>
				<el-form-item label="部门" prop="department">
					<el-input v-model="addForm.department" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import {getContactNumberList,delContactNumber,addContactNumber,editContactNumber} from "@/api/contactNumber/api";
	export default {
		data() {
			return {
				listLoading: false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				//编辑界面数据
				editForm: {
					name: '',   //企业名字
					telNumber: '',  //企业介绍
					department: '',
					id: '', // id
				},
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				//新增界面数据
				addForm: {
					name: '',   //企业名字
					telNumber: '',  //企业介绍
					department: ''
				},
				filters: {
					name: ''
				},
				list: [],
				search: ''
			}
		},
		methods: {
			//编辑
			editSubmit: function () {
				this.$confirm('确认提交吗？', '提示', {}).then(() => {
					let para = Object.assign({}, this.editForm);
					editContactNumber(para).then((res) => {
						if(res.code == 200) {
							this.editLoading = false;
							this.editFormVisible = false;
							this.getContactNumberList();
						}
					});
				});
			},
			//新增
			addSubmit: function () {
				this.$confirm('确认提交吗？', '提示', {}).then(() => {
					this.addLoading = true;
					let para = Object.assign({}, this.addForm);
					addContactNumber(para).then((res) => {
						if(res.code == 200){
							Object.keys(this.addForm).forEach(key => this.addForm[key] = '');
							this.addLoading = false;
							this.addFormVisible = false;
							this.getContactNumberList()
						}
					});
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm.name = row.name;
				this.editForm.telNumber = row.telNumber;
				this.editForm.department = row.department;
				this.editForm.id = row._id;
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			//获取联系电话列表
			getContactNumberList() {
				getContactNumberList().then((res) => {
					this.list = res.data;
				});
			},
			//删除
			handleDel(index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					let para = { id: row._id };
					delContactNumber(para).then((res) => {
						this.getContactNumberList();
					});
				}).catch(() => {

				});
			},
		},
		mounted() {
			this.getContactNumberList();
		}
	}

</script>

<style lang="less">
   .viewNews img{
      width: 100%;
		 height: auto;
    }
</style>
